﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using LikeUs.WebService.Client;
using LikeUs.WebService.Client.WSInfoClient;
using System.Data;
using LikeUs.MovieServices;

namespace LikeUsWebSite.Ajax
{
    /// <summary>
    /// Summary description for services
    /// </summary>
    [WebService(Namespace = "http://localhost:8080/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService]
    public class services : System.Web.Services.WebService
    {
        [System.Web.Services.WebMethod(true)]
        [System.Web.Script.Services.ScriptMethod]
        public string GetHtml(string contextKey)
        {
            MovieApiService likeusApi = new MovieApiService();
            // A little pause to mimic a latent call  
            //System.Threading.Thread.Sleep(250);
            int value = Int32.Parse(contextKey.Split(';')[0]);
            int movieid = Int32.Parse(contextKey.Split(';')[1]);
            string moviename = contextKey.Split(';')[2];
            WSInfoProxy wsProxy = new WSInfoProxy();
            //UserData userData = wsProxy.GetMyUser((string)Session["userName"]);
            UserData userData = (UserData)Session["userData"];
            if (userData == null) return "";
            int userid = (int)userData.UserID;
            if (value > 0)
            {
                wsProxy.linkUserMovie(userid, movieid, value);
                wsProxy.log(userid, userData.Name1 + " has Rated " + moviename + " : " + value);
            }
            DataTable dt = wsProxy.getMoviesLearning(userid, 1);
            LikeUsMovie movie=likeusApi.GetMovieById((int)dt.Rows[0]["idmovies"]);
           string res="<center><img src='" + movie.PosterSmall + "' height='100'/><br/><b>" + movie.Title +
           "</b><br/><font size='1' class='rate'>" +
           "<a href='#' onclick=\"updateDateKey(1," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">1</a> " +
           "<a href='#' onclick=\"updateDateKey(2," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">2</a> " +
           "<a href='#' onclick=\"updateDateKey(3," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">3</a> " +
           "<a href='#' onclick=\"updateDateKey(4," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">4</a> " +
           "<a href='#' onclick=\"updateDateKey(5," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">5</a> " +
           "<a href='#' onclick=\"updateDateKey(6," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">6</a> " +
           "<a href='#' onclick=\"updateDateKey(7," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">7</a> " +
           "<a href='#' onclick=\"updateDateKey(8," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">8</a> " +
           "<a href='#' onclick=\"updateDateKey(9," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">9</a> " +
           "<a href='#' onclick=\"updateDateKey(10," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">10</a> " +
           "<a href='#' onclick=\"updateDateKey(0," + dt.Rows[0]["idmovies"] + ",'" + movie.Title + "')\">?</a></font></center>";
           return res;
        }

        [System.Web.Services.WebMethod(true)]
        [System.Web.Script.Services.ScriptMethod]
        public string GetTopGroupMovies(string contextKey)
        {
            // A little pause to mimic a latent call  
            //System.Threading.Thread.Sleep(250);
            MovieApiService likeusApi = new MovieApiService();
            WSInfoProxy wsProxy = new WSInfoProxy();
            //UserData userData = wsProxy.GetMyUser((string)Session["userName"]);
            UserData userData = (UserData)Session["userData"];
            if (userData == null) return "";
            int userid = (int)userData.UserID;
            DataTable dt = wsProxy.GetTopGroupMovies(userid,Int32.Parse(contextKey));
            List<int> movieId = new List<int>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                movieId.Add((int)dt.Rows[i]["idmovies"]);
            }
            List<LikeUsMovie> moviesList = likeusApi.GetMovieCollection(movieId);
            string result = "<table cellpadding='10'>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                result += "<tr id='row" + i + "'><td><div style='background-color:#EDEFF4;border:1px solid #D8DFEA;border-radius: 15px;'><table width='100%' cellspacing='10' cellpadding='10'><tr><td>"+
                    "<a href='#' onclick=document.getElementById('trailer" + i + "').style.display='block';this.style.display='none'><img src='" + moviesList[i].PosterLarge + "' height='150' style='border:2px solid #4b6c9e;'/></a><div id='trailer" + i + "' style='display:none'>" + "</div></td><td><b>"
                   + moviesList[i].Title + " (" + moviesList[i].Year + ")</b></td><td><p width='200px'>"
                    + moviesList[i].Synopsis + "</p></td><td><h1 style='border-radius: 10px;background-color:#D8DFEA'><center>"
                    + Math.Round((double)dt.Rows[i][1], 1) + "/" + Math.Round((double)moviesList[i].CriticsRating / 10, 1) +
                    "</center></h1></td></tr><tr><td colspan='4'><center><hr></center></td></tr><tr><td colspan='4' style='border-radius: 15px;background-color:#D8DFEA'><center>" +
                    "</b><font size='1' class='rate'>" +
                    "<a href='#' onclick=updateDateKey(1," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>1</a> " +
                    "<a href='#' onclick=updateDateKey(2," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>2</a> " +
                    "<a href='#' onclick=updateDateKey(3," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>3</a> " +
                    "<a href='#' onclick=updateDateKey(4," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>4</a> " +
                    "<a href='#' onclick=updateDateKey(5," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>5</a> " +
                    "<a href='#' onclick=updateDateKey(6," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>6</a> " +
                    "<a href='#' onclick=updateDateKey(7," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>7</a> " +
                    "<a href='#' onclick=updateDateKey(8," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>8</a> " +
                    "<a href='#' onclick=updateDateKey(9," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>9</a> " +
                    "<a href='#' onclick=updateDateKey(10," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>10</a> " +
                    "<a href='#' onclick=updateDateKey(0," + dt.Rows[i][0] + ");checkValue(document.getElementById('row" + i + "'))>?</a></font></center>" +
                    "</center></td></tr></table></div></td></tr>";
            }
            result+="</table>";
            return result;
        }


        [System.Web.Services.WebMethod(true)]
        [System.Web.Script.Services.ScriptMethod]
        public string searchMovieService(string contextKey)
        {
            string result = "";
            List<LikeUsMovie> movieRes;
            result += "Search Results: ";
            MovieApiService likeusApi = new MovieApiService();

            movieRes = likeusApi.SearchMovie(contextKey);

            if (movieRes.Count != 0)
            {
                result += movieRes.Count.ToString()+"<br/>";

                result+="<table cellspacing=2>";
                foreach (LikeUsMovie movie in movieRes)
                {
                    try
                    {
                        result+="<tr><td>" + movie.Id + "<img src='" + movie.PosterSmall + "'/></td><td><b>" + movie.Title + "</b></td><td>" + movie.Synopsis + "</td></tr>";
                    }
                    catch (Exception)
                    {
                        result+="<tr><td>/td><td><b>" + movie.Title + "</b></td><td>" + movie.Synopsis + "</td></tr>";
                    }
                }
                result+="</table>";
            }
            else
            {
                result += "0";
            }
            return result;
        }

    }
}
